package com.esp.webalbum.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;

import com.esp.webalbum.bean.ImageBean;

public class Utils {

	public static String TAG = Utils.class.getCanonicalName();
	
	public static void systemInitialization(){
		try{
			Storage.verifyImagePath();
			
		}catch (Exception e) {
			// TODO: handle exception
			Log.error(TAG, e);
		}
		
	}
	
	public static void systemUpgrade() {
		
		int level = Integer.parseInt(Pref.getValue("LEVEL", "0"));
		switch(level){
			case 0:
				level++;
			case 1:
				//upgradeLevel2();
		}
		
		
		Pref.setValue("LEVEL", level + "");
	}
	
	
	public static boolean isOnline() {
	
		try {
			ConnectivityManager cm = (ConnectivityManager) Const.CONTEXT
					.getSystemService(Context.CONNECTIVITY_SERVICE);
	
			if (cm != null) {
				return cm.getActiveNetworkInfo().isConnectedOrConnecting();
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}
	
	
	public static String getDeviceID(Context context) {
		return ((TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
	}
	
	
	public static String doPost(String url, ArrayList<BasicNameValuePair> params)
			throws Exception {
	
		String strXML = null;
		HttpEntity httpentity = null;
	
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
	
		httppost.setEntity(new UrlEncodedFormEntity(params));
		HttpResponse response = httpclient.execute(httppost);
	
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
	
			httpentity = response.getEntity();
			strXML = new String(EntityUtils.toString(httpentity));
		}
	
		// release
		httpentity = null;
		response = null;
		httppost = null;
		httpclient = null;
	
		return strXML;
	}
	
	public static String doGet(String strURL) throws Exception {
		HttpEntity httpentity = null;
		HttpGet httpGet = null;
		String strResponse = null;
		HttpClient httpclient = new DefaultHttpClient();
		
		Log.debug(TAG,"doGet() |=>> "  +  strURL);
		
		httpGet = new HttpGet(strURL);	
		HttpResponse response = httpclient.execute(httpGet);
		
		
		Log.debug(TAG,"doGet() |=>> Status Code "+ response.getStatusLine().getStatusCode());
		
		if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
			httpentity = response.getEntity();
			strResponse = new String(EntityUtils.toString(httpentity));
	    }
				
		Log.debug(TAG, "::doGet() |=>> Response : " + strResponse);
		
		
		//Release
		response = null;
		httpclient = null;
		httpGet = null;
		httpentity = null;
		
		return strResponse;
	}
	
	public static String readStream(InputStream is) throws IOException {
		int ch = 0;
		String str = new String();
	
		while ((ch = is.read()) != -1) {
			str += (char) ch;
		}
	
		is.close();
	
		return str;
	}
	
	public static void CopyStream(InputStream is, OutputStream os)
    {
        final int buffer_size=1024;
        try
        {
            byte[] bytes=new byte[buffer_size];
            for(;;)
            {
              int count=is.read(bytes, 0, buffer_size);
              if(count==-1)
                  break;
              os.write(bytes, 0, count);
            }
        }
        catch(Exception ex){}
    }
	
	public static int indexOfArray(String[] strArray, String strFind) {
		int index;
	
		for (index = 0; index < strArray.length; index++)
			if (strArray[index].equals(strFind))
				break;
	
		return index;
	}
	
	public static String[] arrListToArray(ArrayList<ImageBean> arrList) 
	{
		String[] objStr = null;
		try 
		{
			if (arrList != null) 
			{
				objStr = new String[arrList.size()];
				int index = 0;
				for (ImageBean obj : arrList) 
				{
					objStr[index] = obj.getStrAbsPath();
					index ++;
				}
			}
	
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		return objStr;
	
	}
	
	

	
}
